package com.yinhai.activity;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.lz.util.Utils;
import com.lz.views.LoadingDialog;
import com.yinhai.adapter.ContactsphoneListAdapter;
import com.yinhai.cfg.LoginConfig;
import com.yinhai.cfg.LoginConfigUtil;
import com.yinhai.constant.UrlConstant;
import com.yinhai.model.Contactsphone;

/**
 * DelContactsPhoneActivity （数据管理）
 * 
 * @author lz
 */
public class DelContactsPhoneActivity extends Activity {
	DelContactsPhoneActivity thisRef;
	AQuery aq = null;
	ContactsphoneListAdapter listadpter = null;
	private LoginConfig log = null;
	private int Index;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_contacts_phone_list);
		thisRef = this;
		aq = new AQuery(this);
		aq.id(R.id.head_title).text("备份数据管理");
		listadpter = new ContactsphoneListAdapter(thisRef, 1);
		aq.id(R.id.lsitview).adapter(listadpter).itemClicked(OnitemClick);
		Load();

	}

	private void Load() {
		log = LoginConfigUtil.getConfig(thisRef);
		String url = UrlConstant.LOCAL_URI + "listBackContacts"; // ?phone=" +  log.getUsername() + "&contacts="+
																	// jsonarry.toString()
		Map<String, String> params = new HashMap<String, String>();
		params.put("phone", log.getUsername());
		aq.ajax(url, params, JSONObject.class, new AjaxCallback<JSONObject>() {
			@Override
			public void callback(String url, JSONObject json, AjaxStatus status) {
				try {
					if (json != null) {
						if (json.getString("flag").equals("1")) {
							JSONArray js = json.getJSONArray("data");
							SimpleDateFormat sdf = new SimpleDateFormat(
									"yyyy-MM-dd HH:mm:ss");
							SimpleDateFormat adf2 = new SimpleDateFormat(
									"yyyy年MM月dd日 HH:mm:ss");
							for (int i = 0; i < js.length(); i++) {
								JSONObject jso = js.getJSONObject(i);
								Contactsphone cs = new Contactsphone();
								cs.setContactsName(adf2.format(sdf.parse(jso
										.getString("contactsName"))));
								cs.setContactsVersion(jso
										.getString("contactsVersion"));
								cs.setConut(js.length());
								listadpter.addItem(cs);
							}
							listadpter.notifyDataSetChanged();
						}

					}

				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}

			}
		});
	}

	OnItemClickListener OnitemClick = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int index,
				long arg3) {
			// TODO Auto-generated method stub
			Contactsphone c = listadpter.getItem(index);
			Index = index;
			String url = UrlConstant.LOCAL_URI + "delBackContacts"; // ?phone=" +  log.getUsername() + "&contacts="+
																	// jsonarry.toString()
			Map<String, String> params = new HashMap<String, String>();
			params.put("contactsVersion", c.getContactsVersion());
			LoadingDialog dialog = new LoadingDialog(thisRef, "正在删备份除数据管理");
			aq.progress(dialog).ajax(url, params, JSONObject.class,
					new AjaxCallback<JSONObject>() {
						@Override
						public void callback(String url, JSONObject json,
								AjaxStatus status) {
							try {
								if (json != null) {
									if (json.getString("flag").equals("1")) {
										Utils.showToast(thisRef, "删除备份数据管理成功");
										listadpter.removeId(Index);
										listadpter.notifyDataSetChanged();
									}
								}

							} catch (Exception e) {
								// TODO: handle exception
								e.printStackTrace();
							}

						}
					});
		}
	};

	public void doBack(View view) {
		onBackPressed();
	}

}
